package com.hj.constants;

import com.hj.dto.CaptchaDto;

import java.text.MessageFormat;

/**
 * @author doudh
 * @date 2020/8/25
 * @description
 */
public interface CaptchaConstants {

    /**
     * @author doudh
     * @date 2020/8/25
     * @description 底图类型枚举
     */
    public enum CaptchaBaseMapEnum {
        ORIGINAL("ORIGINAL", "滑动拼图底图"), SLIDING_BLOCK("SLIDING_BLOCK", "滑动拼图滑块底图"), PIC_CLICK("PIC_CLICK", "文字点选底图");

        private String codeValue;
        private String codeDesc;

        private CaptchaBaseMapEnum(String codeValue, String codeDesc) {
            this.codeValue = codeValue;
            this.codeDesc = codeDesc;
        }

        public String getCodeValue() {
            return this.codeValue;
        }

        public String getCodeDesc() {
            return this.codeDesc;
        }

        /**
         * 根据codeValue获取枚举
         *
         * @param codeValue
         * @author doudh
         * @date 2020/9/9 16:05
         */
        public static CaptchaBaseMapEnum parseFromCodeValue(String codeValue) {
            for (CaptchaBaseMapEnum e : CaptchaBaseMapEnum.values()) {
                if (e.codeValue.equals(codeValue)) {
                    return e;
                }
            }
            return null;
        }

        /**
         * 根据codeValue获取描述
         *
         * @param codeValue
         * @author doudu
         * @date 2020/9/9 16:06
         */
        public static String getCodeDescByCodeValue(String codeValue) {
            CaptchaBaseMapEnum enumItem = parseFromCodeValue(codeValue);
            return enumItem == null ? "" : enumItem.getCodeDesc();
        }

        /**
         * 验证codeValue是否有效
         *
         * @param codeValue
         * @author doudh
         * @date 2020/9/9 16:06
         */
        public static boolean validateCodeValue(String codeValue) {
            return parseFromCodeValue(codeValue) != null;
        }

        /**
         * 列出所有值字符串
         *
         * @param
         * @author doudh
         * @date 2020/9/9 16:07
         */
        public static String getString() {
            StringBuffer buffer = new StringBuffer();
            for (CaptchaBaseMapEnum e : CaptchaBaseMapEnum.values()) {
                buffer.append(e.codeValue).append("--").append(e.getCodeDesc()).append(", ");
            }
            buffer.deleteCharAt(buffer.lastIndexOf(","));
            return buffer.toString().trim();
        }

    }

    /**
     * @author doudh
     * @date 2020/8/25
     * @description 验证码类型
     */
    public enum CaptchaTypeEnum {
        /**
         * 滑块拼图.
         */
        BLOCKPUZZLE("blockPuzzle", "滑块拼图"),
        /**
         * 文字点选.
         */
        CLICKWORD("clickWord", "文字点选");

        private String codeValue;
        private String codeDesc;

        /**
         * 获取实现类beanName
         *
         * @param codeValue
         * @author doudh
         * @date 2020/9/9 16:07
         */
        public static String getServiceBeanName(String codeValue) {
            switch (codeValue) {
                case "blockPuzzle":
                    return "blockPuzzleCaptchaServiceImpl";
                case "clickWord":
                    return "clickWordCaptchaServiceImpl";
                default:
                    return null;
            }
        }

        private CaptchaTypeEnum(String codeValue, String codeDesc) {
            this.codeValue = codeValue;
            this.codeDesc = codeDesc;
        }

        public String getCodeValue() {
            return this.codeValue;
        }

        public String getCodeDesc() {
            return this.codeDesc;
        }

        /**
         * 根据codeValue获取枚举
         *
         * @param codeValue
         * @author doudh
         * @date 2020/9/9 16:08
         */
        public static CaptchaTypeEnum parseFromCodeValue(String codeValue) {
            for (CaptchaTypeEnum e : CaptchaTypeEnum.values()) {
                if (e.codeValue.equals(codeValue)) {
                    return e;
                }
            }
            return null;
        }

        /**
         * 根据codeValue获取描述
         *
         * @param codeValue
         * @author doudh
         * @date 2020/9/9 16:08
         */
        public static String getCodeDescByCodeValue(String codeValue) {
            CaptchaTypeEnum enumItem = parseFromCodeValue(codeValue);
            return enumItem == null ? "" : enumItem.getCodeDesc();
        }

        /**
         * 验证codeValue是否有效
         *
         * @param codeValue
         * @author doudh
         * @date 2020/9/9 16:08
         */
        public static boolean validateCodeValue(String codeValue) {
            return parseFromCodeValue(codeValue) != null;
        }

        /**
         * 列出所有值字符串
         *
         * @param
         * @author doudh
         * @date 2020/9/9 16:08
         */
        public static String getString() {
            StringBuffer buffer = new StringBuffer();
            for (CaptchaTypeEnum e : CaptchaTypeEnum.values()) {
                buffer.append(e.codeValue).append("--").append(e.getCodeDesc()).append(", ");
            }
            buffer.deleteCharAt(buffer.lastIndexOf(","));
            return buffer.toString().trim();
        }

    }

    /**
     * @author doudh
     * @date 2020/8/25
     * @description 返回应答码2Captcha
     */
    public enum RepCodeEnum {

        SUCCESS("0000", "成功"), ERROR("0001", "操作失败"), EXCEPTION("9999", "服务器内部异常"),

        BLANK_ERROR("0011", "{0}不能为空"), NULL_ERROR("0011", "{0}不能为空"), NOT_NULL_ERROR("0012", "{0}必须为空"),
        NOT_EXIST_ERROR("0013", "{0}数据库中不存在"), EXIST_ERROR("0014", "{0}数据库中已存在"), PARAM_TYPE_ERROR("0015", "{0}类型错误"),
        PARAM_FORMAT_ERROR("0016", "{0}格式错误"),

        API_CAPTCHA_INVALID("6110", "验证码已失效，请重新获取"), API_CAPTCHA_COORDINATE_ERROR("6111", "验证失败"),
        API_CAPTCHA_ERROR("6112", "获取验证码失败,请联系管理员"), API_CAPTCHA_BASEMAP_NULL("6113", "底图未初始化成功，请检查路径"),

        ;

        private String code;
        private String desc;

        RepCodeEnum(String code, String desc) {
            this.code = code;
            this.desc = desc;
        }

        public String getCode() {
            return code;
        }

        public String getDesc() {
            return desc;
        }

        public String getName() {
            return this.name();
        }

        /**
         * 将入参fieldNames与this.desc组合成错误信息 {fieldName}不能为空
         *
         * @param fieldNames
         * @return
         */
        public CaptchaDto parseError(Object... fieldNames) {
            CaptchaDto errorMessage = new CaptchaDto();
            String newDesc = MessageFormat.format(this.desc, fieldNames);

            errorMessage.setRepCode(this.code);
            errorMessage.setRepMsg(newDesc);
            return errorMessage;
        }

    }

    /**
     * 密码登陆错误次数
     */
    String PASSWORD_LOGIN_ERROR_TIME_MOBILE_ = "password_login_error_time_mobile:";
    /**
     * 密码登陆错误次数过期时间(单位秒)
     */
    Long PASSWORD_LOGIN_ERROR_TIME_MOBILE_EXPIRE_TIME = 10 * 60L;
    /**
     * 密码登陆是否错误多次
     */
    String PASSWORD_LOGIN_ERROR_MANY_TIME_MOBILE_ = "password_login_error_many_time_mobile:";
    /**
     * 密码登陆是否错误多次过期时间(单位秒)
     */
    Long PASSWORD_LOGIN_ERROR_MANY_TIME_MOBILE_EXPIRE_TIME = 5 * 60L;

    /**
     * 验证码登陆错误次数
     */
    String MSGCODE_LOGIN_ERROR_TIME_MOBILE_ = "msgcode_login_error_time_mobile:";
    /**
     * 验证码登陆错误次数过期时间(单位秒)
     */
    Long MSGCODE_LOGIN_ERROR_TIME_MOBILE_EXPIRE_TIME = 10 * 60L;
    /**
     * 验证码登陆是否错误多次
     */
    String MSGCODE_LOGIN_ERROR_MANY_TIME_MOBILE_ = "msgcode_login_error_many_time_mobile:";

}
